package com.marvels.financial.collection.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.DateUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.dto.CollectListConditionDTO;
import com.marvels.financial.collection.dto.LoanAfteManageDTO;
import com.marvels.financial.collection.service.LoanAfteManageService;


/**
 * 贷后管理
 * @author tangpw
 *
 */
@RequestMapping("financial/collection/loanAfteManage/service")
@RestController
public class LoanAfteManageController {
	
	@Autowired
	private LoanAfteManageService loanAfteManageService;
	
	/**
	 * 贷后管理列表
	 * @return
	 */
	@RequestMapping("loanAfterList")
	@ResponseBody
	public MarvelsResultDTO loanAfterList(@RequestBody CollectListConditionDTO condition){
		try {
			List<LoanAfteManageDTO> list=loanAfteManageService.loanAfterList(condition);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>贷后管理列表查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>贷后管理列表查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "贷后管理列表查询异常", null);
		}
	}
	
	
	/**
	 * excel下载
	 * @param condition
	 * @param request
	 * @param response
	 */
	@RequestMapping("downLoadExcel")
	public void downLoadExcel(CollectListConditionDTO condition,HttpServletRequest request,HttpServletResponse response) {

		BufferedOutputStream output = null;
		ByteArrayOutputStream byteArrayOutputStream=null;
		try {
			String fileName = "逾期清单.xls";
			HSSFWorkbook wb = loanAfteManageService.downLoadExcel(condition);

			byteArrayOutputStream = new ByteArrayOutputStream();
			wb.write(byteArrayOutputStream);
			byte[] bs = byteArrayOutputStream.toByteArray();
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			output = new BufferedOutputStream(response.getOutputStream());
			// 中文文件名必须转码为 ISO8859-1,否则为乱码
			String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
			// 作为附件下载
			response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
			
			output.write(bs);
			byteArrayOutputStream.flush();
			byteArrayOutputStream.close();
			response.flushBuffer();
			output.close();
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载逾期清单--查询列表异常:", e);
		}
		finally {
			if(byteArrayOutputStream!=null){
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常byteArrayOutputStream", e);
				}
			}
			if (output != null)
				try {
					output.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常output", e);
				}
		}
		
	}
	
}
